﻿using System;
using System.Collections.Generic; 
using System.Text;
using ATSModel;
using ATSIDAL;
using System.Data.SqlClient;
using System.Data;
using DBUtility;

namespace ATSDAL
{
    public class PrintRecordDal : BaseRepository_DT<PrintRecordInfo>, IPrintRecord
    {

        #region 静态变量定义部分
        private const string PARM_PRECORD_PRODUCTTYPEID = "@ProductSN";
        private const string PARM_PRECORD_PCIP = "@PCIP";
        private const string PARM_PRECORD_RECORDTIME = "@RecordTime";
        private const string PARM_PRECORD_USERID = "@UserId"; 
        private const string PARM_PRECORD_TEMPNAME = "@TempName";
        private const string PARM_PRECORD_REMARK = "@Remark"; 
        #endregion

        //-------------------------------------用于标签打印软件上传数据录入的Web Service-----------------------------------------------------
        /// <summary>
        /// 上传数据
        /// </summary> 
        /// <returns></returns>
        public bool PrintRecord_Insert(PrintRecordInfo ti)
        { 
            SqlParameter[] Parms = GetParameters();
            SetParameters(ti, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm); 

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "PrintRecord_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 根据SN 下载打印记录集合
        /// </summary>
        /// <param name="productSN"></param>
        /// <returns></returns>
        ///（标签打印软件对接） 
        public IList<PrintRecordInfo> PrintRecord_GetbySN(string productSN)
        {
            SqlParameter parm = GetParameter(PARM_PRECORD_PRODUCTTYPEID);
            parm.Value = productSN;
            IList<PrintRecordInfo> PList = new List<PrintRecordInfo>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "TPrintRecord_select_SN", parm))
            {
                while (rdr.Read())
                {
                    PrintRecordInfo Entity = new PrintRecordInfo();
                    Entity.ProductSN = rdr.GetValue(0).ToString();
                    Entity.PCIP = rdr.GetValue(1).ToString();
                    Entity.RecordTime = Convert.ToDateTime(rdr.GetValue(2));
                    Entity.UserId = rdr.GetValue(3).ToString();
                    Entity.TempName = rdr.GetValue(4).ToString();
                    Entity.Remark = rdr.GetValue(5).ToString();
                    PList.Add(Entity);
                }
            }

            return PList;
        }

   


        #region 重写重用基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override PrintRecordInfo GetEntityRecord(SqlDataReader rdr)
        {
            PrintRecordInfo pro = new PrintRecordInfo
            {
                ProductSN = rdr.GetValue(0).ToString(),
                PCIP = rdr.GetDateTime(1).ToString(),
                RecordTime = Convert.ToDateTime(rdr.GetValue(2)),
                UserId = rdr.GetValue(3).ToString(),
                TempName = rdr.GetValue(4).ToString(),
                Remark = rdr.GetValue(5).ToString()
            };
            return pro;
        }

        private static SqlParameter GetParameter(string parmname)
        {
            SqlParameter parm = new SqlParameter(parmname, SqlDbType.NVarChar, 50);
            return parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_PRECORD_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRECORD_PCIP, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRECORD_RECORDTIME, SqlDbType.DateTime), 
					new SqlParameter(PARM_PRECORD_USERID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRECORD_TEMPNAME, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRECORD_REMARK, SqlDbType.NVarChar, 50) 
                };
            return parms;
        }

        public override void SetParameters(PrintRecordInfo pr, params SqlParameter[] Parms)
        {
            Parms[0].Value = pr.ProductSN;
            Parms[1].Value = pr.PCIP;
            Parms[2].Value = pr.RecordTime;
            Parms[3].Value = pr.UserId;
            Parms[4].Value = pr.TempName;
            Parms[5].Value = pr.Remark; 
        }
        #endregion


        #region IBaseRepository<PrintRecordInfo> 成员


        public PrintRecordInfo LoadEntity<T1>(T1 Id)
        {
            throw new NotImplementedException();
        }

        public IList<PrintRecordInfo> LoadEntities()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
